/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.domain.KwPublicExam;
import me.zhengjie.repository.KwPublicExamRepository;
import me.zhengjie.service.KwPublicExamService;
import me.zhengjie.service.dto.KwPublicExamDto;
import me.zhengjie.service.dto.KwPublicExamQueryCriteria;
import me.zhengjie.service.mapstruct.KwPublicExamMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author LongYang
* @date 2021-06-28
**/
@Service
@RequiredArgsConstructor
public class KwPublicExamServiceImpl implements KwPublicExamService {

    private final KwPublicExamRepository kwPublicExamRepository;
    private final KwPublicExamMapper kwPublicExamMapper;

    @Override
    public Map<String,Object> queryAll(KwPublicExamQueryCriteria criteria, Pageable pageable){
        Page<KwPublicExam> page = kwPublicExamRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(kwPublicExamMapper::toDto));
    }

    @Override
    public List<KwPublicExamDto> queryAll(KwPublicExamQueryCriteria criteria){
        return kwPublicExamMapper.toDto(kwPublicExamRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public KwPublicExamDto findById(Integer id) {
        KwPublicExam kwPublicExam = kwPublicExamRepository.findById(id).orElseGet(KwPublicExam::new);
        ValidationUtil.isNull(kwPublicExam.getId(),"KwPublicExam","id",id);
        return kwPublicExamMapper.toDto(kwPublicExam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public KwPublicExamDto create(KwPublicExam resources) {
        return kwPublicExamMapper.toDto(kwPublicExamRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(KwPublicExam resources) {
        KwPublicExam kwPublicExam = kwPublicExamRepository.findById(resources.getId()).orElseGet(KwPublicExam::new);
        ValidationUtil.isNull( kwPublicExam.getId(),"KwPublicExam","id",resources.getId());
        kwPublicExam.copy(resources);
        kwPublicExamRepository.save(kwPublicExam);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            kwPublicExamRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<KwPublicExamDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (KwPublicExamDto kwPublicExam : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", kwPublicExam.getTitle());
            map.put("考试信息", kwPublicExam.getInfo());
            map.put("删除标识（0：未删除，1：已删除）", kwPublicExam.getDelFlag());
            map.put("副标题", kwPublicExam.getSubTitle());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}